package com.green.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 操作类型
 * @author hxy
 * @date 2012-3-22
 * 
 */
public enum OperateTypeEnum {

	ADD("新增", "ADD"), UPDATE("修改", "UPDATE"), 
	VIEW("查看", "VIEW"), DEL("删除", "DEL");

	private OperateTypeEnum(String alias, String value) {
		this.alias = alias;
		this.value = value;
	}

	private String alias; // 别名
	private String value; // 值

	public String getAlias() {
		return alias;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public static OperateTypeEnum getEnumById(String value) {
		OperateTypeEnum[] enums = OperateTypeEnum.values();
		for (OperateTypeEnum houseStatusEnum : enums) {
			if (houseStatusEnum.getValue().equals(value)) {
				return houseStatusEnum;
			}
		}
		return null;
	}

	/**
	 * 取枚举的json字符串
	 * 
	 * @return
	 */
	public static String getJsonStr(List<OperateTypeEnum> enums) {
		StringBuffer jsonStr = new StringBuffer("[");
		for (OperateTypeEnum houseStatusEnum : enums) {
			if (!"[".equals(jsonStr.toString())) {
				jsonStr.append(",");
			}
			jsonStr.append("{id:'").append(houseStatusEnum).append("',name:'")
					.append(houseStatusEnum.getAlias()).append("',value:'")
					.append(houseStatusEnum).append("'}");
		}
		jsonStr.append("]");
		return jsonStr.toString();
	}

	/**
	 * 取枚举的json字符串
	 * 
	 * @return
	 */
	public static String getJsonStr(OperateTypeEnum houseStatusEnum) {
		StringBuffer jsonStr = new StringBuffer("");
		jsonStr.append("{id:'").append(houseStatusEnum).append("',name:'")
				.append(houseStatusEnum.getAlias()).append("',value:'")
				.append(houseStatusEnum).append("'}");
		return jsonStr.toString();
	}

	/**
	 * 取枚举的json字符串
	 * 
	 * @return
	 */
	public static String getJsonStr() {
		OperateTypeEnum[] enums = OperateTypeEnum.values();
		StringBuffer jsonStr = new StringBuffer("[");
		for (OperateTypeEnum enumTemp : enums) {
			if (!"[".equals(jsonStr.toString())) {
				jsonStr.append(",");
			}
			jsonStr.append("{id:'").append(enumTemp).append("',name:'")
					.append(enumTemp.getAlias()).append("',value:'")
					.append(enumTemp).append("'}");
		}
		jsonStr.append("]");
		return jsonStr.toString();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List toList() {
		OperateTypeEnum[] ary = OperateTypeEnum.values();
		List type = new ArrayList();
		for (int i = 0; i < ary.length; i++) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("key", ary[i].toString());
			map.put("description", ary[i].getAlias());
			type.add(map);
		}
		return type;
	}

}
